package com.nf.health.app.utils;

import android.content.Context;

import com.nf.health.app.models.UserDeviceEntity;

import java.util.concurrent.atomic.AtomicReference;

import static com.nf.health.app.models.UserDeviceEntity.BloodBodyfatDevice;
import static com.nf.health.app.models.UserDeviceEntity.BloodBodyfatTwoDevice;
import static com.nf.health.app.models.UserDeviceEntity.BloodGlucosDevice;
import static com.nf.health.app.models.UserDeviceEntity.BloodPressDevice;
import static com.nf.health.app.models.UserDeviceEntity.ElectrocardDevice;
import static com.nf.health.app.models.UserDeviceEntity.OxygenDevice;
import static com.nf.health.app.models.UserDeviceEntity.SweatDevice;
import static com.nf.health.app.models.UserDeviceEntity.TemperatureDevice;
import static com.nf.health.app.models.UserDeviceEntity.UrinetestDevice;

/**
 * 设备管理类
 * authoer:yyh
 */
public class UserDeviceManager {
    private static ACache aCache;
    private static final AtomicReference<UserDeviceManager> INSTANCE = new AtomicReference<UserDeviceManager>();
    private static DeviceCallback deviceCallback;
    private UserDeviceManager(){}
    //单例模式
    public static  UserDeviceManager getInstance() {
        for (;;) {
            UserDeviceManager current = INSTANCE.get();
            if (current != null) {
                return current;
            }
            current = new UserDeviceManager();
            if (INSTANCE.compareAndSet(null, current)) {
                return current;
            }
        }
    }

    /**
     * 加载设备信息的详情数据
     *
     */
    public void loadDeviceData(Context cnt){
        if(deviceCallback != null){
            deviceCallback.getBloodPressDeviceListener(getBloodPressDevice(cnt));
            deviceCallback.getBloodGlucosDeviceListener(getBloodGlucosDevice(cnt));
            deviceCallback.getBloodBodyfatDeviceListener(getBloodBodyfatDevice(cnt));
            deviceCallback.getBloodBodyfatTwoDeviceListener(getBloodBodyfatTwo(cnt));
            deviceCallback.getUrinetestDeviceListener(getUrinetestDevice(cnt));
            deviceCallback.getOxygenDeviceListener(getOxygenDevice(cnt));
            deviceCallback.getSweatDeviceListener(getSweatDevice(cnt));
            deviceCallback.getTemperatureDeviceListener(getTemperatureDevice(cnt));
            deviceCallback.getElectrocardDeviceListener(getElectrocardDevice(cnt));
        }
    }
    public void regisitDeviceCallback(DeviceCallback deviceCallback){
        this.deviceCallback = deviceCallback;
    }
    //void getBloodPressDeviceListener(String deviceAdr,String deviceName,boolean isBind);
    public interface DeviceCallback{
        void getBloodPressDeviceListener(BloodPressDevice bloodPressDevice);
        void getBloodGlucosDeviceListener(BloodGlucosDevice bloodGlucosDevice);
        void getBloodBodyfatDeviceListener(BloodBodyfatDevice bloodBodyfatDevice);
        void getBloodBodyfatTwoDeviceListener(BloodBodyfatTwoDevice bloodBodyfatTwpDevice);
        void getUrinetestDeviceListener(UrinetestDevice urinetestDevice);
        void getOxygenDeviceListener(OxygenDevice oxygenDevice);
        void getSweatDeviceListener(SweatDevice sweatDevice);
        void getTemperatureDeviceListener(TemperatureDevice temperatureDevice);
        void getElectrocardDeviceListener(ElectrocardDevice electrocardDevice);
    }

    /**
     * 保存血压计的model
     * @param context
     * @param model
     */
    public static void saveBloodPressDevice(Context context,BloodPressDevice model){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        aCache.remove(UserDeviceEntity.BloodPressDevice);
        aCache.put(UserDeviceEntity.BloodPressDevice, model);
        if(deviceCallback != null){
            deviceCallback.getBloodPressDeviceListener(model);
        }
    }
    public static BloodPressDevice getBloodPressDevice(Context context){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        BloodPressDevice model = (BloodPressDevice) aCache.getAsObject(UserDeviceEntity.BloodPressDevice);
        if(model == null) {
            model = new BloodPressDevice();
        }
        return model;
    }

    /**
     * 保存血压计的model
     * @param context
     * @param model
     */
    public static void saveBloodGlucosDevice(Context context,BloodGlucosDevice model){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        aCache.remove(UserDeviceEntity.BloodGlucosDevice);
        aCache.put(UserDeviceEntity.BloodGlucosDevice, model);
        if(deviceCallback != null){
            deviceCallback.getBloodGlucosDeviceListener(model);
        }
    }
    public static UserDeviceEntity.BloodGlucosDevice getBloodGlucosDevice(Context context){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        BloodGlucosDevice model = (BloodGlucosDevice) aCache.getAsObject(UserDeviceEntity.BloodGlucosDevice);
        if(model == null) {
            model = new BloodGlucosDevice();
        }
        return model;
    }
    /**
     * 保存体脂称的model
     * @param context
     * @param model
     */
    public static void saveBloodBodyfatDevice(Context context,BloodBodyfatDevice model){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        aCache.remove(UserDeviceEntity.BloodBodyfatDevice);
        aCache.put(UserDeviceEntity.BloodBodyfatDevice, model);
        if(deviceCallback != null){
            deviceCallback.getBloodBodyfatDeviceListener(model);
        }
    }/**
     * 保存身高体重秤的model
     * @param context
     * @param model
     */
    public static void saveBloodBodyfatTwoDevice(Context context,BloodBodyfatTwoDevice model){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        aCache.remove(UserDeviceEntity.BloodBodyfatTwoDevice);
        aCache.put(UserDeviceEntity.BloodBodyfatTwoDevice, model);
        if(deviceCallback != null){
            deviceCallback.getBloodBodyfatTwoDeviceListener(model);
        }
    }

    public static BloodBodyfatDevice getBloodBodyfatDevice(Context context){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        BloodBodyfatDevice model = (BloodBodyfatDevice) aCache.getAsObject(UserDeviceEntity.BloodBodyfatDevice);
        if(model == null) {
            model = new BloodBodyfatDevice();
        }
        return model;
    }
    public static BloodBodyfatTwoDevice getBloodBodyfatTwo(Context context){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        BloodBodyfatTwoDevice model = (BloodBodyfatTwoDevice) aCache.getAsObject(UserDeviceEntity.BloodBodyfatTwoDevice);
        if(model == null) {
            model = new BloodBodyfatTwoDevice();
        }
        return model;
    }
    /**
     * 保存尿检仪的model
     * @param context
     * @param model
     */
    public static void saveUrinetestDevice(Context context,UrinetestDevice model){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        aCache.remove(UserDeviceEntity.UrinetestDevice);
        aCache.put(UserDeviceEntity.UrinetestDevice, model);
        if(deviceCallback != null){
            deviceCallback.getUrinetestDeviceListener(model);
        }
    }
    public static UrinetestDevice getUrinetestDevice(Context context){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        UrinetestDevice model = (UrinetestDevice) aCache.getAsObject(UserDeviceEntity.UrinetestDevice);
        if(model == null) {
            model = new UrinetestDevice();
        }
        return model;
    }
    /**
     * 保存汗液仪的model
     * @param context
     * @param model
     */
    public static void saveSweatDevice(Context context,UserDeviceEntity.SweatDevice model){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        aCache.remove(UserDeviceEntity.SweatDevice);
        aCache.put(UserDeviceEntity.SweatDevice, model);
        if(deviceCallback != null){
            deviceCallback.getSweatDeviceListener(model);
        }
    }
    public static SweatDevice getSweatDevice(Context context){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        SweatDevice model = (SweatDevice) aCache.getAsObject(UserDeviceEntity.SweatDevice);
        if(model == null) {
            model = new SweatDevice();
        }
        return model;
    }
    /**
     * 保存血痒的model
     * @param context
     * @param model
     */
    public static void saveOxygenDevice(Context context,UserDeviceEntity.OxygenDevice model){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        aCache.remove(UserDeviceEntity.OxygenDevice);
        aCache.put(UserDeviceEntity.OxygenDevice, model);
        if(deviceCallback != null){
            deviceCallback.getOxygenDeviceListener(model);
        }
    }
    public static OxygenDevice getOxygenDevice(Context context){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        OxygenDevice model = (OxygenDevice) aCache.getAsObject(UserDeviceEntity.OxygenDevice);
        if(model == null) {
            model = new OxygenDevice();
        }
        return model;
    }
    /**
     * 保存体温计的model
     * @param context
     * @param model
     */
    public static void saveTemperatureDevice(Context context,UserDeviceEntity.TemperatureDevice model){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        aCache.remove(UserDeviceEntity.TemperatureDevice);
        aCache.put(UserDeviceEntity.TemperatureDevice, model);
        if(deviceCallback != null){
            deviceCallback.getTemperatureDeviceListener(model);
        }
    }
    public static TemperatureDevice getTemperatureDevice(Context context){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        TemperatureDevice model = (TemperatureDevice) aCache.getAsObject(UserDeviceEntity.TemperatureDevice);
        if(model == null) {
            model = new TemperatureDevice();
        }
        return model;
    }
    /**
     * 保存心电图的model
     * @param context
     * @param model
     */
    public static void saveElectrocardDevice(Context context,UserDeviceEntity.ElectrocardDevice model){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        aCache.remove(UserDeviceEntity.ElectrocardDevice);
        aCache.put(UserDeviceEntity.ElectrocardDevice, model);
        if(deviceCallback != null){
            deviceCallback.getElectrocardDeviceListener(model);
        }
    }
    public static ElectrocardDevice getElectrocardDevice(Context context){
        if(aCache == null){
            aCache = ACache.get(context);
        }
        ElectrocardDevice model = (ElectrocardDevice) aCache.getAsObject(UserDeviceEntity.ElectrocardDevice);
        if(model == null) {
            model = new ElectrocardDevice();
        }
        return model;
    }
}
